﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace IngredSubTool
{
    /// <summary>
    /// Interaction logic for SearchView.xaml
    /// </summary>
    public partial class SearchView : UserControl, IAppPage
    {

        public SearchView()
        {
            InitializeComponent();
            Menu.Content = new SideMenu();
            CategoryGrid.Visibility = System.Windows.Visibility.Hidden;
            CategoryGrid.BorderThickness = new Thickness(0);

            //Populate datagrid with categories stored in DataLoader.
            CategoryGrid.ItemsSource = DataLoader.SearchCategories;
            //Load last search term, if there was one.
            SearchField.Text = MainWindow.session.searchTerm;
        }

        private void DoSearch(object sender, RoutedEventArgs e)
        {
            MainWindow.session.searchTerm = SearchField.Text;
            PageSwitcher.Switch(new SearchResultsView());
        }

        private void ToggleCategories(object sender, RoutedEventArgs e)
        {
            Boolean categoryIsVisible = CategoryGrid.Visibility.Equals(Visibility.Visible);
            if (categoryIsVisible)
            {
                CategoryGrid.Visibility = Visibility.Hidden;
                CategoriesBtn.Content = "Show Categories";
            }
            else
            {
                CategoryGrid.Visibility = Visibility.Visible;
                CategoriesBtn.Content = "Hide Categories";
            }
        }

        private void CategoryGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            CategoryGrid.UnselectAllCells();
        }

        private void SearchField_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter){
                DoSearch(sender, e);
            }
        }
    }
}
